package com.happle.gcmclient.utility;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.provider.Settings;

public class DateTimeUtility {

	/**
	 * get long value for current datetime
	 * 
	 * @return
	 */
	private static long getCurrentLongValueForDate() {
		return (new Date()).getTime();
	}

	/**
	 * Convert current date time to string
	 * 
	 * @param updateTime
	 * @return
	 */
	public static String convertNowToFullDateString() {
		SimpleDateFormat dateformat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		// dateformat.setTimeZone(TimeZone.getTimeZone("GMT"));

		// Calendar calendar =
		// Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		// return dateformat.format(calendar.getTime());
		return dateformat.format(new Date());
	}

	/**
	 * Re Format date with predefine format and input date in string
	 * 
	 * @param dateFormat
	 * @param outputFormat
	 * @param dateInput
	 * @return
	 */

	public static String reformatDateTime(String dateFormat,
			String outputFormat, String dateInput) {
		String outputStr = "";

		String aDateFormat = dateFormat.equalsIgnoreCase("") ? "yyyy-MM-dd HH:mm:ss"
				: dateFormat;
		SimpleDateFormat inputFormat = new SimpleDateFormat(aDateFormat);
		SimpleDateFormat outputForm = new SimpleDateFormat(outputFormat);

		Date startDate;
		try {
			startDate = inputFormat.parse(dateInput);
			outputStr = outputForm.format(startDate);

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return outputStr;

	}

	/**
	 * Get date string with format from long value (time interval in second from
	 * 1970)
	 * 
	 * @param dateFormat
	 * @param outputFormat
	 * @param dateInput
	 * @return
	 */
	public static String getDateStringFromLongValue(String dateFormat,
			long timeStamp) {
		String outputStr = "";
		String aDateFormat = dateFormat.equalsIgnoreCase("") ? "yyyy-MM-dd HH:mm:ss"
				: dateFormat;
		SimpleDateFormat inputFormat = new SimpleDateFormat(aDateFormat);

		Date startDate;
		startDate = new Date(timeStamp);

		outputStr = inputFormat.format(startDate);

		return outputStr;

	}

	/**
	 * Re Format date with user's phone format and input date in string
	 * 
	 * @param dateFormat
	 * @param outputFormat
	 * @param dateInput
	 * @return
	 */
	public static String reformatDateTimeWithPhoneSetting(Context context,
			String dateFormat, String dateInput) {
		String ouputFormat = Settings.System.getString(
				context.getContentResolver(), Settings.System.DATE_FORMAT);
		return reformatDateTime(dateFormat, ouputFormat, dateInput);
	}

	/**
	 * Re Format date with user's phone format and input date in string
	 * 
	 * @param dateFormat
	 * @param outputFormat
	 * @param dateInput
	 * @return
	 */
	public static String reformatDateTimeWithPhoneSetting(Context context,
			Date dateInput) {

		Format dateFormat = android.text.format.DateFormat
				.getDateFormat(context);
		String pattern = ((SimpleDateFormat) dateFormat).toLocalizedPattern();

		SimpleDateFormat outputForm = new SimpleDateFormat("HH:mm " + pattern);
		return outputForm.format(dateInput);
	}
}
